package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.common;

import android.content.Context;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.base.BaseViewMode;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.RechargeOrderDetialBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.ConverterHelper;
import com.zzsy.carosshopprojects.util.FormatTimeUtil;
import com.zzsy.carosshopprojects.util.StringUtils;
import com.zzsy.carosshopprojects.util.sunmiprint.PrintHelper;

import java.math.BigDecimal;

import androidx.lifecycle.MutableLiveData;

public class RechargeSuccessMode extends BaseViewMode {
    private MutableLiveData<RechargeOrderDetialBean> liveData;

    public MutableLiveData<RechargeOrderDetialBean> getLiveData() {
        if (liveData == null)
            liveData = new MutableLiveData<>();
        return liveData;
    }

    /**
     * 查询充值订单详情
     *
     * @param orderNumber
     */
    public void getOrderDetialData(Context context, String orderNumber) {
        RequestUtils.GetROrder(context, orderNumber, new MyObserver<RechargeOrderDetialBean>(context) {
            @Override
            public void onSuccess(RechargeOrderDetialBean bean) {
                liveData.postValue(bean);

            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context, errorMsg);
            }
        });
    }

    /**
     * 查询是否可以打印小票
     *
     * @param context
     * @param bean
     */
    public void IsPrintTicket(Context context, RechargeOrderDetialBean bean, int kind) {
        RequestUtils.UpdatePrintStatus(context, bean.getRechargeNumber(), new MyObserver<BaseBean>(context, false) {
            @Override
            public void onSuccess(BaseBean result) {

            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                if (code == ConfigureFile.code) {
                    printMemberRechargeTicket(bean,false);

                } else {
                    ToastUtils.showToast(context, errorMsg);
                }
            }
        });
    }

    /**
     * 打印充值小票
     */
    public void printMemberRechargeTicket(RechargeOrderDetialBean bean,boolean isReprint) {
        if (bean != null) {
            String orderNumber = bean.getRechargeNumber();
            String orderCreateTime = String.valueOf(bean.getOrderCreateTime());
            String time = "";
            if (!StringUtils.isEmpty(orderCreateTime)) {
                time = FormatTimeUtil.formatTimeStamp2DateStr(String.valueOf(Long.parseLong(orderCreateTime) / 1000), "yyyy-MM-dd HH:mm:ss");
            }
            String orderAmount = bean.getAmount();

            String payType = bean.getPayType();
            //获取会员余额
                        /*  int dieselOilAmount = baseBean.getData().getGiveDieselOilAmount();
                        int gasolineAmount = baseBean.getData().getGiveGasolineAmount();
                        String memberBlanceAmounts = baseBean.getData().getMemberBlanceAmounts();
                        String memberCouponAmounts = baseBean.getData().getMemberCouponAmounts();
                        String couponBalance = baseBean.getData().getCouponBalance();
                        */
            String oilType = "";
            if (bean.getProductId() != null) {
                if (bean.getProductId().equals("1")) {
                    oilType = "柴油";
                } else if (bean.getProductId().equals("2")) {
                    oilType = "汽油";
                } else if (bean.getProductId().equals("25")) {
                    oilType = "LNG";
                } else if (bean.getProductId().equals("26")) {
                    oilType = "CNG";
                }
            }
            String redPackedAmount = bean.getRedPackedAmount();
            String redPackedSum = bean.getRedPackedSum();
            if (StringUtils.isEmpty(redPackedAmount)) {
                redPackedAmount = "0";
            }

            String memberBlanceAmounts = bean.getRechargeBalance();

            if (StringUtils.isEmpty(memberBlanceAmounts)) {
                memberBlanceAmounts = "0";
            }

            String goodsCouponAmount = bean.getGoodsCouponAmount();
            if (StringUtils.isEmpty(goodsCouponAmount)) {
                goodsCouponAmount = "0";
            }

            String payTypeStr = "";
            if ("1".equals(payType)) {
                payTypeStr = "支付宝支付";
            } else if ("2".equals(payType)) {
                payTypeStr = "微信支付";
            } else if ("3".equals(payType)) {
                payTypeStr = "银联支付";
            } else if ("4".equals(payType)) {
                payTypeStr = "会员支付";
            } else if ("5".equals(payType)) {
                payTypeStr = "现金支付";
            }
            String storeName = bean.getStoreName();
            String memberPhone = bean.getPhone();

            double orderAmountDou = ConverterHelper.toBigDecimal(orderAmount).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            double memberBlanceAmountsDou = ConverterHelper.toBigDecimal(memberBlanceAmounts).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            double redPackedAmountDou = ConverterHelper.toBigDecimal(redPackedAmount).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            double redPackedSumDou = ConverterHelper.toBigDecimal(redPackedSum).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            double goodsCouponAmountDou = ConverterHelper.toBigDecimal(goodsCouponAmount).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

            PrintHelper.getInstance().PrintRechargeOrder(orderNumber, time,orderAmountDou+"",storeName,payTypeStr,memberBlanceAmountsDou,memberPhone,redPackedAmountDou,goodsCouponAmountDou,redPackedSumDou,oilType,bean.getReductionCouponAmount(),isReprint);
        }
    }
}
